package com.example.capsule_chat0322.request;

import android.content.Context;
import android.os.Handler;

import com.example.capsule_chat0322.bean.SignBean;
import com.example.capsule_chat0322.configuration.HttpConstant;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.utils.AlterUtils;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @Author zhuzhiming
 * @Date 2022/5/20 17:40
 */
public class SignRequest {

    public static void judgeHasSign(Handler handler, String roomName, String userId) {


        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .url(HttpConstant.HTTP_PREFIX + "/judgeHasSign/" + roomName + "/" + userId)
                .build();

        Call call = httpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                AlterUtils.alertByToastInSonThread((Context) GlobalAssistMap.getObject("context"), "查询个人信息失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if(response.code() != 200){
                    return;
                }

                String resBody = response.body().string();
                SignBean signBean = new Gson().fromJson(resBody, SignBean.class);

                if (signBean == null) {
                    return;
                }
                handler.sendMessage(handler.obtainMessage(1, signBean));

            }
        });


    }


    public static void doSign(SignBean signBean) {

        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , new Gson().toJson(signBean));

        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .post(requestBody)
                .url(HttpConstant.HTTP_PREFIX + "/sign")
                .build();

        httpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });


    }

}
